package com.framework.service.yhbservice.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.user.YhbRecommendMerchantDao;
import com.framework.entity.push.PushConfig;
import com.framework.entity.push.PushContent;
import com.framework.entity.sys.SysAgent;
import com.framework.service.AgentService;
import com.framework.service.PushConfigService;
import com.framework.service.yhbservice.YhbRecommendMerchantService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.PackUtils;
import com.framework.yhbEntity.user.YhbRecommendMerchant;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.view.CusTranView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service("yhbRecommendMerchantService")
public class YhbRecommendMerchantServiceImpl extends ServiceImpl<YhbRecommendMerchantDao,YhbRecommendMerchant> implements YhbRecommendMerchantService {

    @Autowired
    private YhbUserService yhbUserService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private PushConfigService pushConfigService;

    @Override
    public List<CusTranView> getMerchantList(String token, Integer pageNo) {
        YhbUser user = yhbUserService.queryUserByToken(token);

        Integer start=(pageNo-1)*10;
        List<Map<String,Object>> list=baseMapper.getMerchantList(user.getUserId(),start);

      //  return getLastMonthsListByDate(list);
        //封装然后返回
        return PackUtils.packToCusTranView(list,"createDate");
    }

    @Override
    public List<Map<String,Object>> getYhbMerchant(Long agentId, Integer state) {
        return baseMapper.getYhbMerchant(agentId,state);
    }

    @Override
    public YhbRecommendMerchant queryRecommendBySn(String sn) {
        YhbRecommendMerchant rm=new YhbRecommendMerchant();
        rm.setSn(sn);
        return baseMapper.selectOne(rm);
    }

    @Override
    public Map<String, Object> getMerchantInfo(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        //分润总金额
        String totalAmount=baseMapper.queryRecommendAmount(user.getUserId());
        double temp=Double.valueOf(totalAmount);

        int amount= (int) temp;


        totalAmount=String.format("%04d", amount);
        String[] chars = totalAmount.split("");

        int index=chars.length;

        //渠道下的所有推荐商户返现
        List<Map<String,Object>> recommendList=baseMapper.queryRecommendList(user.getDeptId());

        //过往推荐记录
        List<YhbRecommendMerchant> merchantlist=baseMapper.selectList(new EntityWrapper<YhbRecommendMerchant>().eq("user_id",user.getUserId()).eq("state",1));

        Map<String,Object> map=new HashMap<>();
        map.put("one",chars[index-1]);
        map.put("ten",chars[index-2]);
        map.put("hun",chars[index-3]);
        map.put("tho",chars[index-4]);
        map.put("flowList",recommendList);
        map.put("merchantlist",merchantlist);
        return map;
    }

    @Override
    public Map<String,Object> recommendMerchant(String token, Long agentId, String name, String phone) {
        YhbUser user = yhbUserService.queryUserByToken(token);

        YhbRecommendMerchant recommendMerchant=new YhbRecommendMerchant();
        recommendMerchant.setState(2);
        recommendMerchant.setAgentId(agentId);
        recommendMerchant.setPhone(phone);
        recommendMerchant.setName(name);
        recommendMerchant.setCreateDate(new Date());
        recommendMerchant.setUserId(user.getUserId());

        SysAgent agent = agentService.queryAgentById(agentId);
        if(agent==null ){
            recommendMerchant.setIsBaiyi(0);
            baseMapper.insert(recommendMerchant);
        }else {
            recommendMerchant.setIsBaiyi(1);
            baseMapper.insert(recommendMerchant);

            PushConfig config = pushConfigService.queryPushConfigByDeptId(agent.getDeptId());

            PushContent content=new PushContent();
            content.setNoticeType("ass");
            content.setAssType(2);
            content.setAlias(new String[]{String.valueOf(agentId)});
            content.setAlert("您有一位新的推荐商户");
            content.setTitle("推荐商户");
            //JiGuangPush.jiguangPush(content,config);

        }
        Map<String,Object> map=new HashMap<>();
        map.put("msg","推荐成功,请点击记录查看");
        return map;
    }

    @Override
    public Integer getYhbMerchantCount(Long agentId, Integer state) {
        return baseMapper.getYhbMerchantCount(agentId,state);
    }


}
